<?php
  session_start();
  if (!isset($_SESSION["email"])) {
      header('Location: login.php');
  }else{
    if ($_SESSION["email"]!="programming.iter@gmail.com") {
       header('Location: login.php');
    }
  }
?>
<?php include("incs/top.php"); ?>

    <div class="container-fluid">
      <div class="row">
        <?php include("incs/sidebar.php"); ?>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Quản lý thành viên</h1>
          <div class="row placeholders">
              
              <button style="display:none" type="button" id="btn_back" class="btn btn-warning">&nbsp;&nbsp;&nbsp;Quay lại&nbsp;&nbsp;&nbsp;</button>
          
          </div>
          <?php if (!isset($_GET["member_id"])):
            
          ?>
          <h3 class="sub-header">Danh sách thành viên</h3>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>Email</th>
                  <th>Họ tên</th>
                  <th>Ngày đăng ký</th>
                  <th>Action</th>
                </tr>
              </thead>
              <tbody>
                <?php
                  include("../lib/connection.php");
                  $sql = "SELECT * FROM member ORDER BY upd_date desc";
                  $query = mysqli_query($conn,$sql);
                  $TRs = "";
                  while ($row = mysqli_fetch_array($query)) {
                    $id = $row["id"];
                    $email = $row['email'];
                    $name = $row['name'];
                    $upd_date = $row['upd_date'];
                    $TRs .= "<tr><td>$id</td><td><a name='member_$id' href='#'>$email</a> </td><td>$name</td><td>$upd_date</td><td><a name='delete_$id' href='#'>Xóa người dùng</a></td></tr>";
                  }
                  echo $TRs;
                ?>
             
              </tbody>
            </table>
          </div>
          <?php endif;?>
          <?php if (isset($_GET["member_id"])): ?>
          
          <div class="table-responsive">
             <form id="fr_member" name="fr_member" action="act/process_save_member.php" method="POST">
            <h3 class="page-header">Chi tiết người dùng</h3>
            <?php 
              include("../lib/connection.php");
              $id = $_GET["member_id"];
              $sql = "SELECT *
                      FROM member
                      WHERE id=$id";
              $query = mysqli_query($conn,$sql);
              $row = mysqli_fetch_array($query);

            ?>
            <input type="hidden" value="<?php echo $row['id'];?>" name="member_id" >
            <div class="alert alert-danger" id="error" role="alert" style="display:none"></div>
            <div class="row placeholders">
              <div class="col-sm-2" align="left"><label class="label label-info">Cấp độ :</label></div>
              <div class="col-sm-2" align="left">
                  <!-- <input type="text" value="<?php echo $row["cat_name"] ?>"> -->
                  <select id="level"name="level">
                    <option value="0" <?php echo ($row['level']==0)?"selected='selected'":"" ?>>Member</option>
                    <option value="1" <?php echo ($row['level']==1)?"selected='selected'":"" ?>>Admin</option>
                  </select>
              </div>
              <div class="col-sm-8"></div>
            </div>
            <div class="row placeholders">
              <div class="col-sm-2" align="left"><label class="label label-info">Email :</label></div>
              <div class="col-sm-2">
                <input type="text" size="60" readonly="readonly" id="email" name="email" value="<?php echo $row["email"] ?>"> 
              </div>
              <div class="col-sm-8"></div>
            </div>
            <div class="row placeholders">
              <div class="col-sm-2" align="left"><label class="label label-info">Họ tên:</label></div>
              <div class="col-sm-2">
                <input type="text" size="60" id="name" name="name" value="<?php echo $row["name"] ?>"> 
              </div>
              <div class="col-sm-8"></div>
            </div>
            <div class="row placeholders">
              <div class="col-sm-2" align="left"><label for="short_content" class="label label-info">Trạng thái :</label></div>
              <div class="col-sm-2" align="left">
                <select id="status"name="status">
                    <option value="0" <?php echo ($row['status']==0)?"selected='selected'":"" ?>>Chưa duyệt</option>
                    <option value="1" <?php echo ($row['status']==1)?"selected='selected'":"" ?>>Duyệt</option>
                </select>
              </div>
              <div class="col-sm-8"></div>
            </div>
            <div class="table-responsive">
              <br />
              <button id="btn_update" name="btn_update" class="btn btn-primary btn-lg">Cập nhật</button>
            </div>
          </form>
          </div>
        <?php endif;?>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../bootstrap/js/docs.min.js"></script>
    <script src="ckeditor/ckeditor.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">
      var check = <?php echo isset($_GET['member_id'])?0:1 ?>;
      if (check==0) {
        $("#btn_back").show();
      }
      $("a[name^='member_']").on("click",function(){
        var name = $(this).attr("name");
        var array = name.split("_");
        var id = array[1];
        window.location.assign("quan-ly-thanh-vien.php?member_id="+id);
      });
      $("#btn_back").on("click",function(){
        window.location.assign("quan-ly-thanh-vien.php");
      });
      $("a[name^='delete_']").on("click",function(){
        var name = $(this).attr("name");
        var array = name.split("_");
        var id = array[1];
        window.location.assign("act/process_save_member.php?member_id="+id);
      });
    </script>
   
  </body>
</html>